package com.googlecode.icecube.push.game;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.googlecode.icefusion.ui.commons.BackingBeanForm;
import com.googlecode.icefusion.ui.commons.dialog.IMessageDialog;


/**
 * Manages a single game player.
 * 
 * @author Rainer Eschen
 * 
 */
public class Game extends BackingBeanForm {

    @Autowired
    private MultiUserGame multiUserGame;

    /**
     * Alias for the player in the game.
     */
    private String name;

    /**
     * Reference to player instance in the game.
     */
    private Player player;

    /**
     * Country to check for a hit.
     */
    private String country;

    /**
     * Handling of "Player can not be added to the game"
     */
    private PlayerNotAddedMessage playerNotAddedMessage = this.new PlayerNotAddedMessage();

    /**
     * Handling of "Country of other Player hit"
     */
    private PlayerHitMessage playerHitMessage = this.new PlayerHitMessage();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * Player tries to become part of the game.
     * 
     * @return navigationId
     */
    public String addPlayer() {
        try {
            this.setPlayer(this.multiUserGame.addPlayer(this.name));
            // Maybe the name was adapted
            this.setName(this.getPlayer().getName());
            this.multiUserGame.push();
        } catch (NotEnoughCountriesException e) {
            this.getPlayerNotAddedMessage().setRendered(true);
        }
        return null;
    }

    /**
     * Player leaves the game.
     * 
     * @return navigationId
     */
    public String deletePlayer() {

        this.multiUserGame.deletePlayer(this.getPlayer());
        this.setPlayer(null);
        this.setName(null);
        this.multiUserGame.push();
        return null;
    }

    /**
     * true: Player has lost the game. Check button will be disabled.
     * 
     * @return loser status
     */
    public Boolean getLoser() {

        return this.multiUserGame.getLoser(this.getPlayer());
    }

    /**
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    public void setPlayer(Player player) {
        this.player = player;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * Check suggested Country for a hit in other player's country lists.
     * 
     * @return navigationId
     */
    public String checkCountry() {
        Player playerHit = this.multiUserGame.checkCountry(this.getPlayer(), this.getCountry());
        if (playerHit != null) {
            this.playerHitMessage.setRendered(true);
            this.multiUserGame.push();
        }
        return null;
    }

    public List<Map.Entry<String, String>> getCountriesMap() {

        return this.multiUserGame.getCountriesMap(this.getPlayer());
    }

    public String getCountries() {

        return this.multiUserGame.getCountries(this.getPlayer());
    }

    public String getCountriesHit() {

        return this.multiUserGame.getCountriesHit(this.getPlayer());
    }

    public String getLosers() {

        return this.multiUserGame.getLosers(this.getPlayer());
    }

    public PlayerNotAddedMessage getPlayerNotAddedMessage() {
        return playerNotAddedMessage;
    }

    public void setPlayerNotAdded(PlayerNotAddedMessage playerNotAddedMessage) {
        this.playerNotAddedMessage = playerNotAddedMessage;
    }

    public PlayerHitMessage getPlayerHitMessage() {
        return playerHitMessage;
    }

    public void setPlayerHitMessage(PlayerHitMessage playerHit) {
        this.playerHitMessage = playerHitMessage;
    }

    // inner classes for messageDialogs

    /**
     * Manages event handling of messageDialog "Player not added"
     * 
     */
    public class PlayerNotAddedMessage implements IMessageDialog {

        private static final long serialVersionUID = -7497931929348669151L;

        /**
         * true: show this dialog
         */
        private Boolean rendered = false;

        /**
         * @see com.googlecode.icefusion.ui.commons.dialog.IMessageDialog#messageDialogButtonOk()
         */
        public String messageDialogButtonOk() {
            this.setRendered(false);
            setName(null);
            return null;
        }

        public Boolean getRendered() {
            return rendered;
        }

        public void setRendered(Boolean rendered) {
            this.rendered = rendered;
        }
    }

    /**
     * Manages event handling of messageDialog "Country hit"
     * 
     */
    public class PlayerHitMessage implements IMessageDialog {

        private static final long serialVersionUID = 5991991159565632707L;

        /**
         * true: show this dialog
         */
        private Boolean rendered = false;

        /**
         * @see com.googlecode.icefusion.ui.commons.dialog.IMessageDialog#messageDialogButtonOk()
         */
        public String messageDialogButtonOk() {
            this.setRendered(false);
            multiUserGame.push();
            return null;
        }

        public Boolean getRendered() {
            return rendered;
        }

        public void setRendered(Boolean rendered) {
            this.rendered = rendered;
        }
    }
}
